<?php
/**
 * Created by PhpStorm.
 * User: sid.lee
 * Date: 2019/12/05
 * Email: lx.xin@qq.com
 */

if (! function_exists('flash')) {
    function flash($message, $level = 'warning')
    {
        $message = new \Illuminate\Support\MessageBag([$level => $message]);
        request()->session()->flash($level, $message);
    }
}

if (! function_exists('get_first_flash')) {
    /**
     * 获取第一条FLASH信息.
     *
     * @param $level
     *
     * @return mixed|string
     */
    function get_first_flash($level)
    {
        if ($level == 'error' && session('errors') && session('errors')->any()) {
            return session('errors')->all()[0];
        }
        if (! session()->has($level)) {
            return '';
        }

        return session($level)->first();
    }
}


if(! function_exists('')){
    function get_aether_upload_url($value){
        if (empty($value)) return 'https://via.placeholder.com/400x400.png?text=default+img';
        if(starts_with($value,'http://') or starts_with($value,'https://')){
            return $value;
        }
        return env('AETHER_UPLOAD_HOST') . $value;
    }
}



if (! function_exists('menu_is_active')) {
    /**
     * 指定路由名是否与当前访问的路由名相同.
     *
     * @param $routeName
     *
     * @return bool
     */
    function menu_is_active(int $menuId)
    {
        $currentUrl = trim(request()->url());
        $menu = \App\Models\Menu::find($menuId);
        if (! $menu) {
            return false;
        }
        $children = $menu->children;
        $filter = function (string $url) {
            $url = route($url);//str_replace(['index', '/index'], '', $url);

            return trim($url);
        };
        if ($children->isEmpty()) {
            $url = $filter($menu->url);
            return preg_match("#{$url}#", $currentUrl);
        }
        foreach ($children as $child) {
            $url = $filter($child->url);
            $result = preg_match("#{$url}#", $currentUrl);
            if ($result) {
                return true;
            }
        }

        return false;
    }
}

if (! function_exists('backend_menus')) {
    /**
     * 获取当前管理员的专属菜单.
     *
     * @return array|mixed
     */
    function backend_menus()
    {
        $user = admin();

        if (! $user) {
            return collect([]);
        }
        if ($user->isSuper()) {
            return (new \App\Models\Menu())->menus();
        }

        $permissions = $user->getAllPermissions()->pluck('name');
        $menus = \App\Models\Menu::with('children')
                                              ->whereIn('permission_name', $permissions)
                                              ->rootLevel()
                                              ->orderAsc()
                                              ->get();
        $menus = $menus->filter(function ($menu) use ($permissions) {
            if ($menu->children->isEmpty()) {
                return false;
            }
            $permissions = $permissions->toArray();
            $children = $menu->children->filter(function ($child) use ($permissions) {
                return in_array($child->permission_name, $permissions);
            });
            $menu->children = $children;

            return $children->count() != 0;
        });

        return $menus;
    }

    if (! function_exists('admin')) {
        /**
         * 获取当前登录的管理员.
         *
         * @return \App\Models\Adin
         */
        function admin()
        {
            return \Illuminate\Support\Facades\Auth::guard('admin')->user();
        }
    }
}
